<?php
/**
 * @name IndexController
 * @author root
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
class UserController extends Yaf_Controller_Abstract {
	public function indexAction(){
		return $this->loginAction();
	}


	public function loginAction(){
		$submit = $this->getRequest()->getQuery("submit", "0");
		if($submit!="1"){
		    echo json_encode(['errno'=>-1001, 'errmsg'=>'参数错误']);exit;
        }

        //获取参数
        $uname = $this->getRequest()->getPost("uname", false);
		$pwd = $this->getRequest()->getPost("pwd", false);
		if(!$uname || !$pwd){
		    echo json_encode(['errno'=>-1002, 'errmsg'=>'用户名或者密码不能为空']);
		    return false;
        }

        //调用Model，做登录验证
        $model = new UserModel();
		$uid = $model->login(trim($uname), trim($pwd));
		if($uid){
		    //存入session
            session_start();
            $_SESSION['user_token'] = md5("salt".$_SERVER['REQUEST_TIME'].$uid);
            $_SESSION['user_token_time'] = $_SERVER['REQUEST_TIME'];
            $_SESSION['user_id'] = $uid;

		    echo json_encode(['errno'=>0, 'errmsg'=>'', 'data'=>['name'=>$uname]]);
        }else{
		    echo json_encode(['errno'=>$model->errno, 'errmsg'=>$model->errmsg]);
        }

        return true;
	}

	public function registerAction(){
		//获取参数
		$uname = $this->getRequest()->getPost("uname", false);
		$pwd = $this->getRequest()->getPost("pwd", false);
		$mobile = $this->getRequest()->getPost("mobile", false);

		if( !$uname || !$pwd ){
			echo json_encode( ["errno"=>-1002, "errmsg"=>"用户名与密码不能为空"] );exit;
		}

		//调用Model,做登录验证
		$model = new UserModel();
		if( $model->register( trim($uname), trim($pwd), trim($mobile) ) ){
			echo json_encode( [
				"errno" => 0,
				"errmsg" => "",
				"data" => ["name"=>$uname]
			]);exit;
		}else{
			echo json_encode([
				"errno" => $model->errno,
				"errmsg" => $model->errmsg,
			]);exit;
		}

		return true;
	}

	public function sendSmsAction(){
        $mobile = $this->getRequest()->getPost('mobile', false);

        if(!$mobile){
            echo json_encode(['errno'=>1009, 'errmsg'=>'手机号不能为空']);exit;
        }

        $model = new UserModel();
        if($model->sendSms($mobile)){
            echo json_encode(['errno'=>0, 'errmsg'=>'成功发送验证码']);exit;
        }else{
            echo json_encode(['errno'=>$model->errno, 'errmsg'=>$model->errmsg]);exit;
        }
    }

	public function findPwdAction(){
        $mobile = $this->getRequest()->getPost('mobile', false);
        $smsCode = $this->getRequest()->getPost('smsCode', false);
	    $newPwd = $this->getRequest()->getPost('newPwd', false);
	    $reNewPwd = $this->getRequest()->getPost('reNewPwd', false);

	    if($newPwd!==$reNewPwd){
	        echo json_encode(['errno'=>1108, 'errmsg'=>'两次密码不一致']);exit;
        }else if(strlen($newPwd)<8){
            echo json_encode(['errno'=>1109, 'errmsg'=>'密码长度不能小于8位']);exit;
        }

	    $model = new UserModel();
	    if($model->findPwd($mobile, $smsCode, $newPwd)){
	        echo json_encode(['errno'=>0, 'errmsg'=>'找回密码成功']);exit;
        }else{
	        echo json_encode(['errno'=>1104, 'errmsg'=>'找回密码失败']);exit;
        }
    }
}
